package com.fly.sms;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.fly.sms.progress.ThreadProcess;
import com.fly.sms.service.CustomerAddressBookDetailService;
import com.fly.sms.util.StringUtil;

public class AddressBookDetailAddActivity extends BaseActivity implements
		OnClickListener, OnCheckedChangeListener {
	EditText Name;
	RadioGroup Sex;
	EditText DestNumber;
	Button submitBtn;
	Button cancleBtn;
	int position;
	String AddressBookId = "0";
	/** Called when the activity is first created. */
	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View view = this.getView(R.layout.addressbookdetailedit);
		content.addView(view);
		title.setText(getText(R.string.addressbookdetailadd));
		Name = (EditText) findViewById(R.id.Name);
		Sex = (RadioGroup) findViewById(R.id.Sex);
		DestNumber = (EditText) findViewById(R.id.DestNumber);
		submitBtn = (Button) findViewById(R.id.submitBtn);
		cancleBtn = (Button) findViewById(R.id.cancleBtn);
		cancleBtn.setOnClickListener(this);
		submitBtn.setOnClickListener(this);
		Sex.setOnCheckedChangeListener(this);
		AddressBookId = context.get("AddressBookId").toString();
		context.set("Sex", "1");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.submitBtn:
			if (StringUtil.isEmpty(Name.getText().toString())) {
				showMsg(getText(R.string.namenull));
				Name.setFocusable(true);
				Name.setFocusableInTouchMode(true);
				Name.requestFocus();
				return;
			}
			if (StringUtil.isEmpty(DestNumber.getText().toString())) {
				showMsg(getText(R.string.numbernull));
				DestNumber.setFocusable(true);
				DestNumber.setFocusableInTouchMode(true);
				DestNumber.requestFocus();
				return;
			}
			new ThreadProcess(context, new ThreadProcess.onProcess() {
				boolean flag = false;
				@Override
				public void onStart() {
					flag = CustomerAddressBookDetailService.add(AddressBookId, Name
							.getText().toString(), context.get("Sex").toString(), "", DestNumber.getText().toString());
				}

				@Override
				public void onFinished() {
					if (flag) {
						context.showMsg(context.getText(R.string.oprsuccess));
						context.set("remoterefresh", true);
						finish();
					} else {
						context.showMsg(context.getText(R.string.oprfail));
					}
				}
			}).startProgress();
			break;
		case R.id.cancleBtn:
			finish();
			break;
		}
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.male:
			context.set("Sex", "1");
			break;
		case R.id.female:
			context.set("Sex", "0");
		}
	}
}